package com.carrental.api.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrental.api.common.exception.Assert;
import com.carrental.common.exception.OperationException;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.common.utils.CommonQueryWrapper;
import com.carrental.api.oms.domain.OmsReturnCheck;
import com.carrental.api.oms.domain.bo.OmsReturnCheckPost;
import com.carrental.api.oms.mapper.OmsReturnCheckMapper;
import com.carrental.api.oms.service.IOmsReturnCheckService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 出车检查单 服务实现类
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-07 23:12:15
 */
@Service
public class OmsReturnCheckServiceImpl extends ServiceImpl<OmsReturnCheckMapper, OmsReturnCheck> implements IOmsReturnCheckService {
    /**
     * 获取分页
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public IPage<OmsReturnCheck> pageList(CommonQueryParam commonQueryParam) {
        return this.baseMapper.pageList(
                new Page<>(commonQueryParam.getCurrent(), commonQueryParam.getSize()),
                new CommonQueryWrapper<OmsReturnCheck>(commonQueryParam).lambda()
                        .eq(OmsReturnCheck::getIsDeleted, false)
        );
    }

    /**
     * 添加出车检查单
     *
     * @param returnCheckPost 出车检查单参数
     * @return 是否成功
     */
    @Override
    public Boolean create(OmsReturnCheckPost returnCheckPost) {
        if(this.detailByOrderId(returnCheckPost.getOrderId())!=null){
            throw new OperationException("已有还车检查单！请勿重复操作！");
        }
        OmsReturnCheck returnCheck=new OmsReturnCheck();
        BeanUtils.copyProperties(returnCheckPost,returnCheck);
        if (!this.save(returnCheck)) {
            throw new OperationException("创建还车检查单失败！");
        }
        return true;
    }

    /**
     * 逻辑删除
     *
     * @param id id
     * @return 是否成功
     */
    @Override
    public Boolean delete(Long id) {
        //先查询，再更新
        OmsReturnCheck returnCheck = this.getById(id);
        Assert.notNull(returnCheck, "删除失败，无此出车检查单！");
        returnCheck.setIsDeleted(true);
        if (!this.updateById(returnCheck)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }



    /**
     * 修改出车检查单信息
     * @param id id
     * @param returnCheckPost 出车检查单参数
     * @return 是否成功
     */
    @Override
    public Boolean update(Long id, OmsReturnCheckPost returnCheckPost) {
        //先查询，再更新
        OmsReturnCheck returnCheck = this.getById(id);
        Assert.notNull(returnCheck, "修改失败，无此出车检查单！");
        BeanUtils.copyProperties(returnCheckPost, returnCheck);
        if (!this.updateById(returnCheck)) {
            throw new OperationException("添加出车检查单信息失败！");
        }
        return true;
    }

    /**
     * 查看出车检查单信息
     *
     * @param id 出车检查单参数
     * @return 出车检查单信息
     */
    @Override
    public OmsReturnCheck detail(Long id) {
        OmsReturnCheck returnCheck=this.getById(id);
        Assert.notNull(returnCheck,"查询失败，无此出车检查单");
        return returnCheck;
    }

    /**
     * 根绝订单号查看还车检查单信息
     *
     * @param orderId 还车检查单参数
     * @return 还车检查单信息
     */
    @Override
    public OmsReturnCheck detailByOrderId(Long orderId) {
        return this.getOne(
                new QueryWrapper<OmsReturnCheck>().lambda()
                        .eq(OmsReturnCheck::getOrderId,orderId)
                        .eq(OmsReturnCheck::getIsDeleted,false)
        );
    }

    /**
     * 获取列表
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public List<OmsReturnCheck> list(CommonQueryParam commonQueryParam) {
        return this.list(
                new CommonQueryWrapper<OmsReturnCheck>(commonQueryParam).lambda()
                        .eq(OmsReturnCheck::getIsDeleted, false)
        );
    }

}
